package com.mijie.util;

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Locale;

import javax.imageio.ImageIO;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;


public class ImageUtil {

    public static boolean zoom(File src,File dest,int width,int height,boolean proportion,float quality) {   
        try {   
            if (src==null || !src.exists())  return false;   
            
            Image img = ImageIO.read(src);   
            // 判断图片格式是否正确   
            if (img.getWidth(null) == -1) {  
                System.out.println(" can't read,retry!" + "<BR>");   
            } else {   
                int newWidth, newHeight;   
                // 判断是否是等比缩放   
                if (proportion) {   
                    // 为等比缩放计算输出的图片宽度及高度   
                    double rate1 = ((double) img.getWidth(null)) / (double) width + 0.1;   
                    double rate2 = ((double) img.getHeight(null)) / (double) height + 0.1;   
                    // 根据缩放比率大的进行缩放控制   
                    double rate = rate1 > rate2 ? rate1 : rate2;   
                    newWidth = (int) (((double) img.getWidth(null)) / rate);   
                    newHeight = (int) (((double) img.getHeight(null)) / rate);   
                } else {   
                    newWidth = width; // 输出的图片宽度   
                    newHeight = height; // 输出的图片高度   
                }   
               BufferedImage tag = new BufferedImage((int) newWidth, (int) newHeight, BufferedImage.TYPE_INT_RGB);   
                 
               /* 
                * Image.SCALE_SMOOTH 的缩略算法 生成缩略图片的平滑度的 
                * 优先级比速度高 生成的图片质量比较好 但速度慢 
                */   
               tag.getGraphics().drawImage(img.getScaledInstance(newWidth, newHeight, Image.SCALE_SMOOTH), 0, 0, null);  
               FileOutputStream out = new FileOutputStream(dest);  
               JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
               JPEGEncodeParam param = null;
               if(quality>0){
            	   if(quality>1) quality = 1;
	               param = encoder.getDefaultJPEGEncodeParam(tag);
	               param.setQuality(quality, false);
	               encoder.setJPEGEncodeParam(param); 
               }
               encoder.encode(tag);    
               out.close();   
               return true;
            }   
        } catch (IOException ex) {   
            ex.printStackTrace();   
        }   
        return false;   
   }   
   
   public static boolean compressQuality(File src,File dest,float quality){
	   if (src==null || !src.exists())  return false; 
	   try{
		   Image img = ImageIO.read(src);
		   FileOutputStream out = new FileOutputStream(dest);  
           JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
           JPEGEncodeParam param = null;
           if(quality>0){
        	   BufferedImage tag = new BufferedImage(img.getWidth(null), (int) img.getHeight(null), BufferedImage.TYPE_INT_RGB);   
               tag.getGraphics().drawImage(img.getScaledInstance(img.getWidth(null), img.getHeight(null), Image.SCALE_SMOOTH), 0, 0, null);  
        	   if(quality>1) quality = 1;
               param = encoder.getDefaultJPEGEncodeParam(tag);
               param.setQuality(quality, false);
               encoder.setJPEGEncodeParam(param); 
               encoder.encode(tag);  
           }
           out.close();   
		   return true;
	   }catch(Exception e){
		   e.printStackTrace();  
	   }
	   
	   return false;
   }
    
   public static boolean zoomWithFixWidth(File src,File dest,int fixWidth,float quality) {   
        try {   
            if (src==null || !src.exists())  return false;   
            
            Image img = ImageIO.read(src);   
            if (img.getWidth(null) == -1) {  
                System.out.println(" can't read,retry!");   
            } else {
            	if(img.getWidth(null)>fixWidth){
	               int newHeight; 
	               double rate = ((double) img.getWidth(null)) / (double) fixWidth;   
	               newHeight = (int) (((double) img.getHeight(null)) / rate);   
	               BufferedImage tag = new BufferedImage(fixWidth, (int) newHeight, BufferedImage.TYPE_INT_RGB);   
	                 
	               tag.getGraphics().drawImage(img.getScaledInstance(fixWidth, newHeight, Image.SCALE_SMOOTH), 0, 0, null);  
	               FileOutputStream out = new FileOutputStream(dest);  
	               JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
	               JPEGEncodeParam param = null;
	               if(quality>0){
	            	   if(quality>1) quality = 1;
		               param = encoder.getDefaultJPEGEncodeParam(tag);
		               param.setQuality(quality, false);
		               encoder.setJPEGEncodeParam(param); 
	               }
	               encoder.encode(tag);   
	               out.close();   
            	}else{
            		IOUtil.copyFileTo(src, dest);
            	}
               return true;
            }   
        } catch (IOException ex) {   
            ex.printStackTrace();   
        }   
        return false;   
   }   
    
    public static void main(String[] args){
    	File dir = new File("/Users/zhengzb/Downloads/defaultAvatar");
    	File[] files = dir.listFiles();
    	File dest =  null;
    	for(File src : files){
    		dest = new File("/Users/zhengzb/Downloads/smallDefaultAvatar/"+src.getName());
    		ImageUtil.zoomWithFixWidth(src, dest, 150, -1f);
    	}
    	
    	
    }
   
}
